﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Week8UserManagement
{
    public class User
    {
        private string _username;
        private string _password;

        public string Username 
        { 
            get => _username; 
            set => _username = value?.Trim() ?? string.Empty; 
        }

        public string Password 
        { 
            get => _password; 
            set => _password = value ?? string.Empty; 
        }

        public User() { }

        public User(string username, string password)
        {
            Username = username;
            Password = password;
        }
    }

    public interface IUserService
    {
        void Run();
        bool Register();
        bool Login();
        bool IsUsernameValid(string username);
        bool IsPasswordValid(string password);
        bool DeleteUser();
    }

    public class UserService : IUserService
    {
        private readonly List<User> _users = new List<User>();

        public void Run()
        {
            bool isRunning = true;
            while (isRunning)
            {
                Console.Clear();
                Console.WriteLine("===== 第八周用户管理系统 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 删除用户");
                Console.WriteLine("4. 退出");
                Console.WriteLine("==============================");
                Console.Write("请选择功能 (1-4): ");

                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        bool loginSuccess = Login();
                        if (loginSuccess) Console.WriteLine("登录成功，进入系统！");
                        WaitForKey();
                        break;
                    case "2":
                        bool registerSuccess = Register();
                        if (registerSuccess) Console.WriteLine("注册成功！");
                        WaitForKey();
                        break;
                    case "3":
                        bool deleteSuccess = DeleteUser();
                        if (deleteSuccess) Console.WriteLine("用户删除成功！");
                        WaitForKey();
                        break;
                    case "4":
                        isRunning = false;
                        Console.WriteLine("谢谢使用，再见！");
                        break;
                    default:
                        Console.WriteLine("请输入1-4之间的有效数字！");
                        WaitForKey();
                        break;
                }
            }
        }

        public bool Register()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");

            string username = GetValidUsername();
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("注册取消");
                return false;
            }

            string password = GetValidPassword();
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("注册取消");
                return false;
            }

            if (!ConfirmPassword(password))
            {
                Console.WriteLine("密码确认失败，注册取消");
                return false;
            }

            _users.Add(new User(username, password));
            return true;
        }

        public bool Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");

            Console.Write("请输入用户名: ");
            string username = Console.ReadLine()?.Trim() ?? string.Empty;

            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("用户名不可为空！");
                return false;
            }

            User foundUser = null;
            foreach (var user in _users)
            {
                if (user.Username == username)
                {
                    foundUser = user;
                    break;
                }
            }

            if (foundUser == null)
            {
                Console.WriteLine("用户名不存在！");
                return false;
            }

            Console.Write("请输入密码: ");
            string password = Console.ReadLine() ?? string.Empty;
            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("密码不可为空！");
                return false;
            }

            return foundUser.Password == password;
        }

        public bool DeleteUser()
        {
            Console.Clear();
            Console.WriteLine("===== 删除用户 =====");

            if (_users.Count == 0)
            {
                Console.WriteLine("当前无注册用户，无法删除！");
                return false;
            }

            Console.Write("请输入要删除的用户名: ");
            string username = Console.ReadLine()?.Trim() ?? string.Empty;
            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("用户名不可为空！");
                return false;
            }

            for (int i = 0; i < _users.Count; i++)
            {
                if (_users[i].Username == username)
                {
                    Console.Write("请输入该用户密码确认: ");
                    string password = Console.ReadLine() ?? string.Empty;
                    if (_users[i].Password == password)
                    {
                        _users.RemoveAt(i);
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("密码错误，删除失败！");
                        return false;
                    }
                }
            }

            Console.WriteLine("用户名不存在，删除失败！");
            return false;
        }

        public bool IsUsernameValid(string username)
        {
            if (string.IsNullOrWhiteSpace(username)) return false;
            if (username.Length < 3 || username.Length > 15) return false;
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$")) return false;

            foreach (var user in _users)
            {
                if (user.Username == username) return false;
            }

            return true;
        }

        public bool IsPasswordValid(string password)
        {
            if (string.IsNullOrWhiteSpace(password)) return false;
            if (password.Length < 6 || password.Length > 20) return false;
            if (!Regex.IsMatch(password, @"[A-Z]") || !Regex.IsMatch(password, @"[a-z]") || !Regex.IsMatch(password, @"[0-9]"))
                return false;

            return true;
        }

        private string GetValidUsername()
        {
            string username;
            do
            {
                Console.Write("请输入用户名: ");
                username = Console.ReadLine()?.Trim() ?? string.Empty;

                if (!IsUsernameValid(username))
                {
                    if (string.IsNullOrWhiteSpace(username)) Console.WriteLine("用户名不可为空！");
                    else if (username.Length < 3 || username.Length > 15) Console.WriteLine("用户名长度需3-15个字符！");
                    else if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$")) Console.WriteLine("用户名仅含字母、数字、下划线！");
                    else Console.WriteLine("用户名已被注册！");

                    Console.Write("是否重新输入？(y/n): ");
                    string retry = Console.ReadLine()?.Trim().ToLower();
                    if (retry != "y") return string.Empty;
                }
                else break;
            } while (true);

            return username;
        }

        private string GetValidPassword()
        {
            string password;
            do
            {
                Console.Write("请输入密码: ");
                password = Console.ReadLine() ?? string.Empty;

                if (!IsPasswordValid(password))
                {
                    if (string.IsNullOrWhiteSpace(password)) Console.WriteLine("密码不可为空！");
                    else if (password.Length < 6 || password.Length > 20) Console.WriteLine("密码长度需6-20个字符！");
                    else Console.WriteLine("密码需含大小写字母和数字！");

                    Console.Write("是否重新输入？(y/n): ");
                    string retry = Console.ReadLine()?.Trim().ToLower();
                    if (retry != "y") return string.Empty;
                }
                else break;
            } while (true);

            return password;
        }

        private bool ConfirmPassword(string password)
        {
            Console.Write("请确认密码: ");
            string confirmPwd = Console.ReadLine() ?? string.Empty;
            if (confirmPwd != password)
            {
                Console.WriteLine("两次密码不一致！");
                return false;
            }
            return true;
        }

        private void WaitForKey()
        {
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUserService userService = new UserService();
            userService.Run();
        }
    }
} 